package com.ysd.servlet.admin;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.Gson;
import com.ysd.dao.impl.Message;
import com.ysd.dao.impl.UserDaoImpl;
import com.ysd.entity.User;
import com.ysd.util.BaseServlet;
import com.ysd.util.Result;

public class AdminUserServlet extends BaseServlet {
	/**
	 * 查询出留言板的所有数据
	 * 
	 *localhost:8080/meizizi/adminUser?reqName=getMessageByStatus
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void getMessageByStatus(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();

		UserDaoImpl uDao = new UserDaoImpl();

		String status = request.getParameter("status");
		int page = Integer.parseInt(request.getParameter("page"));
		int size = Integer.parseInt(request.getParameter("size"));
		List<User> uList = uDao.getUserByStatus(status, page, size);

		Gson gson = new Gson();
		String res = gson.toJson(uList);
		out.print(res);

		out.flush();
		out.close();
	}
	
	 /**
	    * 查询出留言板的所有数据
	    * @param localhost:8080/meizizi/adminUser?reqName=getMessageByStatusNum
	    * @param response
	    * @throws ServletException
	    * @throws IOException
	    */
	public void getMessageByStatusNum(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		
		UserDaoImpl uDao = new UserDaoImpl();
		
		String status = request.getParameter("status");
		List<User> uList = uDao.getUserByStatusNum(status);
		
		Gson gson = new Gson();
		String res = gson.toJson(uList.size());
		out.print(res);
		
		out.flush();
		out.close();
		
	}

	
	/**
	 * localhost:8080/meizizi/adminUser?reqName=delUserById
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void delUserById(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		Message mes = new Message();// 创建实现类对象
		int mId = Integer.parseInt(request.getParameter("MId"));
		int n = mes.delMessageById(mId);
		response.setCharacterEncoding("UTF-8");// 设置响应的编码格式
		PrintWriter out = response.getWriter();// out 对象
		if (n > 0) {
			out.print(Result.toClient(true, "删除成功", ""));
		} else {
			out.print(Result.toClient(false, "删除失败", ""));
		}

		out.flush();
		out.close();

	}

	/**
	 * localhost:8080/meizizi/adminUser?reqName=upadateMessageById
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void upadateMessageById(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		response.setCharacterEncoding("UTF-8");// 设置响应的编码格式
		PrintWriter out = response.getWriter();// out 对象

		UserDaoImpl uDao = new UserDaoImpl();
		int id = Integer.parseInt(request.getParameter("id"));

		User user = uDao.getUserById(id);
		if (user.getStatus().equals("已回复") || user.getStatus().equals("已读")) {
			out.print(Result.toClient(true, "", ""));
		} else { // 只有未读情况才要 修改
			String status = request.getParameter("status");
			user = new User(id, status, null);

			int n = uDao.updateMessage(user);
			if (n > 0) {
				out.print(Result.toClient(true, "修改成功", ""));
			} else {
				out.print(Result.toClient(false, "修改失败", ""));
			}
		}

		out.flush();
		out.close();
	}

	/**
	 * localhost:8080/meizizi/adminUser?reqName=getMessageById
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void getMessageById(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		response.setCharacterEncoding("UTF-8");// 设置响应的编码格式
		PrintWriter out = response.getWriter();// out 对象

		UserDaoImpl uDao = new UserDaoImpl();
		int id = Integer.parseInt(request.getParameter("id"));
		User user = uDao.getUserById(id);

		Gson gson = new Gson();
		String res = gson.toJson(user);

		out.print(res); // 响应数据

		out.flush();
		out.close();
	}

	/**
	 * 修改回复信息 localhost:8080/meizizi/adminUser?reqName=upadateMessageReplyById
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void upadateMessageReplyById(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		response.setCharacterEncoding("UTF-8");// 设置响应的编码格式
		PrintWriter out = response.getWriter();// out 对象

		UserDaoImpl uDao = new UserDaoImpl();
		int id = Integer.parseInt(request.getParameter("id"));
		String reply = request.getParameter("reply");
		User user = new User(id, null, reply);
		int n = uDao.updateMessageReply(user);

		if (n > 0) {
			out.print(Result.toClient(true, "修改成功", ""));
		} else {
			out.print(Result.toClient(false, "修改失败", ""));
		}

		out.flush();
		out.close();
	}
	/**
	 * 修改回复信息 localhost:8080/meizizi/adminUser?reqName=delMessageReplyByIds
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void delMessageReplyByIds(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		response.setCharacterEncoding("UTF-8");// 设置响应的编码格式
		PrintWriter out = response.getWriter();// out 对象
		String ids=request.getParameter("ids");
		UserDaoImpl uDao = new UserDaoImpl();
		
		int n = uDao.delUserByIds(ids);

		if (n > 0) {
			out.print(Result.toClient(true, "修改成功", ""));
		} else {
			out.print(Result.toClient(false, "修改失败", ""));
		}

		out.flush();
		out.close();
	}
}
